﻿
// 分组信息对象
var groupList;

var lastDiv;

// 延时计时器
var timer;

// 刷新弹出层模板
function refushGroupTemplate() {
    $('.showMore:last').html('');
    for (var i = 0; i < groupList.length; i++) {
        $('.showMore:last').append('<p><input type="checkbox" value="' + groupList[i].GroupId + '" />' + groupList[i].Name + '</p>');
    } 
}

// 刷新分组界面显示
function refushGroup(gid) {
    if (!gid) gid = -1;
    if (!groupList)
        groupList = $.parseJSON($('#MainContent_hGroupList').val());
    var maxCount = 3;
    var liLast = $("#liMore").clone();
    $("#grouplist").html("");
    $("#grouplist").append('<li class="float-l" gid="-1"><span>全部</span></li><li class="float-l" gid="0"><span>未分组</span></li>');
    var count = groupList.length;
    if (count > maxCount) count = maxCount;
    for (var i = 0; i < count; i++) {
        $("#grouplist").append('<li class="float-l" gid="' + groupList[i].GroupId + '"><span>' + groupList[i].Name + '</span></li>');
    }
    if (groupList.length > maxCount) {
        $("#grouplist").append(liLast);
        $("ul", liLast).html("");
        for (var i = 3; i < groupList.length; i++) {
            $("ul", liLast).append('<li gid="' + groupList[i].GroupId + '">' + groupList[i].Name + '</li>');
        }
        liLast.show();
    }

    $('span', $("#grouplist li")).removeClass();
    $('li[gid=' + gid + '] span', $("#grouplist")).removeClass().addClass("grouping-cur fontB colorBlack");
}

/// 外部分组选中查询
$("#grouplist li span[id!=hfMore]").live("click", function () {
    $('span', $("#grouplist li")).removeClass();
    $(this).removeClass().addClass("grouping-cur fontB colorBlack");
    var gid = $(this).parent("li").attr("gid");
    if (gid > 0) {
        $('#divGroupManage').show();
        GetManager(gid);
    } else $('#divGroupManage').hide();
    SearchGroupUserList(1, true);
});

/// 更多分组选中查询
$("#showMores li").live('click', function () {
    var entity;
    for (var i in groupList) {
        if (groupList[i].GroupId == $(this).attr('gid')) {
            entity = groupList[i];
            break;
        }
    }
    groupList.splice($.inArray(entity, groupList), 1);
    groupList.splice(0, 0, entity);

    $("#showMores").hide();

    var gid = $(this).attr("gid");
    refushGroup(gid);
    if (gid > 0) {
        $('#divGroupManage').show();
        GetManager(gid);
    } else $('#divGroupManage').hide();
    SearchGroupUserList(1, true);
});

//显示更多分组层
$("#hfMore").live('mouseover', function () {
    clearTimeout(timer);
    timer = setTimeout(function () {
        $("#showMores").slideDown("show");
    }, 100);
});

//延时隐藏更多分组层（用户可快速滑动鼠标到层上，终止隐藏）
$("#hfMore").live('mouseout', function () {
    clearTimeout(timer);
    timer = setTimeout(function () {
        $("#showMores").slideUp("show");
    }, 100);
});

// 显示用户分组层
$('#Container1 span').live('mouseover', function () {
    clearTimeout(timer);
    var span = $(this);
    timer = setTimeout(function () {
        ShowUserGroupList($("a", span)[0]);
    }, 100);
});

// 延时隐藏用户分组层（用户可快速滑动鼠标到层上，终止隐藏）
$('#Container1 span').live('mouseout', function () {
    clearTimeout(timer);
    var span = $(this);
    timer = setTimeout(function () {
        span.next().slideUp("show");
    }, 100);
});

//保持层显示
$(".showMore").live('mouseover', function () {
    clearTimeout(timer);
});

//隐藏层
$(".showMore").live('mouseout', function () {
    var div = $(this);
    timer = setTimeout(function () {
        div.slideUp("show");
    }, 100);
});

// 终止事件冒泡
function cancelBubble(obj, evt) {
    var e = (evt) ? evt : window.event;
    if (window.event) {
        e.cancelBubble = true;
    } else {
        e.stopPropagation();
    } 
}

// 显示/构造用户分组层
function ShowUserGroupList(obj, evt) {
    //cancelBubble(obj, evt);
    var pList = $(obj).attr('GroupList').split(',');
    if (pList.length < 1) return;
    if (obj != lastDiv) $('.showMore').slideUp("show");
    var div = $('.showMore', $(obj).parent().parent());
    if (div.length <= 0) {
        div = $('.showMore:last').clone();
        var index = 0;
        $.each($('input[type=checkbox]', div), function () {
            this.checked = false;
            for (var i = 0; i < pList.length; i++) {
                if (pList[i] == $(this).val()) {
                    this.checked = true;
                    pList.splice($.inArray(pList[i], pList), 1);
                }
            }
            $(this).click(
                function () {
                    ChangeUserGroup(this, $(obj).attr('userid'));
                });
        });
        $(obj).parent().after(div);
    }
    div.slideToggle("show");
    lastDiv = obj;
}

// 修正分组显示信息
function toGroutListNull(str) {
    if (!str || str == null) return "未分组";
    else {
        var num = 7
        if (str.length > num) {
            return str.substring(0, num - 1) + "...";
        }
        else
            return str;
    }
}

// 修改用户分组
function ChangeUserGroup(input, userid) {
    var url = "/Services/AjaxHandler.ashx?action=Organization.Member";
    $.ajax({
        type: "post",
        url: url,
        data: { type: input.checked == true ? 'add' : 'delete', userid: userid, groupid: input.value },
        success: function () {
            var groupIdList = $(input).parent().parent().prev().find('a').attr("groupList").split(',');
            var groupList = $(input).parent().parent().prev().attr("title").split(',');

            var groupId = input.value;
            var groupName = $(input).parent().text().trim();
            if (input.checked) {
                groupIdList.splice(0, 0, groupId);
                groupList.splice(0, 0, groupName);
            }
            else {
                groupIdList.splice($.inArray(groupId, groupIdList), 1);
                groupList.splice($.inArray(groupName, groupList), 1);
            }
            var str = groupList + '';
            if (str[str.length - 1] == ',') str = str.substring(0, str.length - 1);
            $(input).parent().parent().prev().find('a').attr("groupList", groupIdList);
            var a = $(input).parent().parent().prev().find('a').clone();
            $(input).parent().parent().prev().attr("title", str);
            $(input).parent().parent().prev().text(toGroutListNull(str));
            $(input).parent().parent().prev().append(a);
        }
    });
}

// 得到分组责任人
function GetManager(gid) {
    var url = "/Services/AjaxHandler.ashx?action=Organization.GetManager";
    $.ajax({
        type: "post",
        url: url,
        data: { groupid: gid },
        success: function (result) {
            var jsonData = $.parseJSON(result);
            if (jsonData.Result == 1000) {
                $('em', $('#aModify').parent()).text(jsonData.Data.Name);
            }
            else {
                $('em', $('#aModify').parent()).text('无');
            }
        }
    });
}

// 弹出成员添加页面层
function AddMember(type) {
    var groupid = $('.grouping-cur', $("#grouplist")).parent().attr("gid");
    var options = {
        lock: true,     //是否弹出层遮盖住窗体,
        fixed: true,     //是否把窗口固定在窗口中央,即窗口不会随滚动条滚动
        width:500 + "px",
        title: "",
        close: function () {       //关闭窗口时的回调函数 
            if (art.dialog.data("result") == "ok") {
                art.dialog.removeData("result");
                SearchGroupUserList(1, true);
            }
        }
    };
    if (type == "hospital") {
        art.dialog.open('/doctor/addmember.aspx?groupId=' + groupid, options);
    } else {
        art.dialog.open('/community/addmember.aspx?groupId=' + groupid, options);
    }
}